package Logic;

import Gui.frontend_main;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
import java.util.ArrayList;

/*
 * Simple demo that uses java.util.Timer to schedule a task to execute once 5
 * seconds have passed.
 */

public class timerClass {
	Toolkit toolkit;
	public ArrayList<Ball> array;
	public ArrayList<Integer> xArray = new ArrayList<Integer>();

	Timer timer;
	Timer timerY;
	boolean cycle = false;
	static int count = 0;
	int direction = 1;

	public timerClass(int seconds) {
		toolkit = Toolkit.getDefaultToolkit();
		timer = new Timer();
		timer
				.scheduleAtFixedRate(new MoveXTask(), seconds * 100,
						seconds * 100);

//		timerY = new Timer();
//		timerY.scheduleAtFixedRate(new MoveYTask(), seconds * 200,
//				seconds * 200);

	}

	class MoveXTask extends TimerTask {
		public MoveXTask() {

		}

		public void run() {
			moveBallsX();
		}
	}

	class MoveYTask extends TimerTask {
		public MoveYTask() {

		}

		public void run() {
			moveBallsY();

		}
	}

	/**
	 * method to move balls based on their starting position and collision
	 * levels
	 */
	public void moveBallsX() {

		array = frontend_main.ballList;
		ArrayList<Integer> moveDistX = new ArrayList<Integer>();
		ArrayList<Integer> ylevels = new ArrayList<Integer>();

		int movementX = 0;

		/*
		 * chooses how far the X coordinate changes, the greater the case the
		 * less the movement
		 */

		// get X levels
		if (array.size() >= 2) {
			for (int i = 0; i < array.size(); i++) {
				switch (((Ball) array.get(i)).getXLevel()) {
				case 1:
					movementX = 40;
					break;
				case 2:
					movementX = 30;
					break;
				case 3:
					movementX = 20;
					break;
				case 4:
					movementX = 10;
					break;
				}

				//store movementY
				moveDistX.add(movementX);
				/*
				 * now time to move balls!!!
				 */
			}
			//check y levels
			boolean ylev = true;

				if (array.size() >= 2) {
					for (int i = 0; i < array.size(); i++) {
						switch (((Ball) array.get(i)).getYLevel()) {
						case 1:
							ylev = true;
							break;
						case 2:
							ylev = false;
							break;
						case 3:
							ylev = false;
							break;
						case 4:
							ylev = false;
							break;
						}
						if(ylev == false)
							break;
					}
				}

			//who knows if this will work--hopefully make 00 and 11 work together
			//first bubble sort to sort balls by x coordinate

			/*
			 * now time to move balls!!!
			 */
				System.out.println(ylev);
			if(array.size() > 2){
				if(array.get(0).getCharge() == 11 && array.get(3).getCharge() == 11){
					if(array.get(1).getCharge() == 00){
						if(ylev == true){
							for(int i = 0; i < array.size(); i++){
							//set movement based on ylevels
							moveDistX.set(i,5);
						}


						}
					}
				}

				else if(array.get(0).getCharge() == 01 && array.get(2).getCharge() == 01){
					if(array.get(1).getCharge() == 10){
						if(ylev == true){
							for(int i = 0; i < array.size(); i++){
								moveDistX.set(i, 5);
							}
						}
					}
				}
			}





			if (count <= 4) {
				for (int i = 0; i < array.size(); i++) {
					//make second ball go opposite way
					if(i%2 == 1){
						direction = direction * -1;
					}
					// change x position
					array.get(i).setX(array.get(i).getOrigX()
							+ (direction * count * (moveDistX.get(i) / 8)));
				}
				count++;
			} else {
				direction = direction * -1;
				count = 0;
			}
		}

	}

	public void moveBallsY() {

		array = frontend_main.ballList;
		ArrayList<Integer> moveDistY = new ArrayList<Integer>();

		int movementY = 0;

		// get Y levels
		if (array.size() >= 2) {
			for (int i = 0; i < array.size(); i++) {
				switch (((Ball) array.get(i)).getYLevel()) {
				case 1:
					movementY = 40;
					break;
				case 2:
					movementY = 30;
					break;
				case 3:
					movementY = 20;
					break;
				case 4:
					movementY = 10;
					break;
				}

				//store movementY
				moveDistY.add(movementY);
				/*
				 * now time to move balls!!!
				 */
			}

			if (count <= 4) {
				for (int i = 0; i < array.size(); i++) {
					// change y position
					array.get(i).setY(array.get(i).getOrigY()
							+ (direction * count * (moveDistY.get(i) / 8)));
				}
				count++;
			} else {
				direction = direction * -1;
				count = 0;
			}
		}
	}


}
